package com.example.administrator.personalmail.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.personalmail.Activity.AddressActivity;
import com.example.administrator.personalmail.Activity.CreateOrderActivity;
import com.example.administrator.personalmail.Activity.LoginActivity;
import com.example.administrator.personalmail.MyApplication;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.msg.User;

/**
 * Created by Administrator on 2018/4/14 0014.
 */

public class FragmentFive extends Fragment{
    private Button loginOut;
    private TextView tvLoginName;
    private TextView tvFavorite;
    private TextView tvAddress;
    private TextView tvOrder;
    private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_five,container,false);

        init();
        return view;

    }

    private void init() {
        loginOut = view.findViewById(R.id.btn_logout_mine);
        loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginOut.setVisibility(View.GONE);
                tvLoginName.setText("点击登陆");
                MyApplication application = MyApplication.getInstance();
                User user = application.getUser();
                if (user != null){
                    application.clearUser();
                }
            }
        });
        tvLoginName = view.findViewById(R.id.txt_username_mine);
        tvLoginName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent, Contants.USER_LOGIN);
            }
        });

        tvFavorite = view.findViewById(R.id.txt_my_favorite);
        tvFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        tvOrder = view.findViewById(R.id.txt_my_orders);
        tvOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        tvAddress = view.findViewById(R.id.txt_my_address);
        tvAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyApplication application = MyApplication.getInstance();
                User user = application.getUser();
                if (user == null) {
                    Toast.makeText(getContext(),"请先登陆",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(getActivity(), AddressActivity.class);
                    startActivity(intent);
                }

            }
        });


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Contants.REQUEST_SUCCESS){
            User user = MyApplication.getInstance().getUser();
            if (user != null){
                loginOut.setVisibility(View.VISIBLE);
                tvLoginName.setText(user.getUsername());
            }

        }
    }

    @Override
    public void onResume() {
        super.onResume();

        User user = MyApplication.getInstance().getUser();
        if (user != null){
            tvLoginName.setText(user.getUsername());
            loginOut.setVisibility(View.VISIBLE);
        }
    }
}
